The set_defaults Script

Setting up a consistent diagram design environment can be challenging, because most organizations have at least some standards and conventions of their own that they want to use. Therefore, part of the P&ID program code is loaded and executed from the set_defaults script of the project environment, and each user organization can freely change the code in that file, as long as the interface remains intact.

The set_defaults script can be stored in the library or the project database, and in the CADMATIC example project it can be found from [library] > Resources > Scripts > pd > setup. This file allows the following setup tasks to be configured as appropriate:

  • Pre-fill values when the user is editing the database data of a diagram object.
  • Generate unique IDs for diagram objects.
  • Manage the menu item data of instruments.
  • Get instrument symbol parameters from the database image.

Version Detector

The set_defaults script includes the function call

CadmaticVersion( ) { return( 52 ); }

The return value from this call is the actual CADMATIC version number. The program checks the version number and notifies the user if the version number is too small. Thus, the administrator knows when to update the script.

Pre-filling Database Records

When a diagram designer is inserting a new object to a diagram by using a Diagram Object Template, the template already has some of the data that the new object requires, so it is convenient to pre-fill the relevant data before showing the data fields to the user. The database field "MenuItmData" functions as a key to the data to be added. The CADMATIC example project uses a lookup table (COMPLOOKUP) to store the descriptions in English according to MenuItmData.

When a user is inserting a diagram object whose main database table is ARMATURES, ELECTRICS, EQUIPMENTS or INSTRUMENTS, the application calls the following script function before showing the data editor to the user:

SET_DEFAULTS(string tblname, string type, string sys, string groupname, /* inout: */ listimage, typeimage)

tblname

The name of the main database table, such as ARMATURES.

type

The name of the sub-table, such as CABLES, ILINES, PIPES or VALVES.

sys

The name of the System.

groupname

For ARMATURES and PIPES this is the pipeline ID.
For ELECTRICS this is the electric circuit.
For INSTRUMENTS this is the instrument tag name.
For EQUIPMENTS this argument is an empty string.

listimage

The database image of the main table to be filled. Note that the field MenuItmData is already filled.

typeimage

The database image of the sub-table to be filled.

After the user has edited the data and accepts the changes, the application calls the following script function to check the validity of the data:

CHECK_DATA(string tblname, string type, string sys, string groupname, /* inout: */ listimage, typeimage)

If the return value is not zero, the data is not accepted and the editing restarts.

The CHECK_DATA function is also called when the compare functionality is used to update the P&ID data from the 3D model—see Compare P&ID objects with 3D model.

Managing the ID Generator

The application requires unique names for Position IDs, Pipelines, Instrument Tags and Systems. To allow smooth operation, any unique names must be initialized to the database card before showing it to the user. The user can then either accept the suggested value or change it. The application must check that the entered ID name really is unique.

To initialize unique ID names to database cards, the scriptlib functions call a set of script functions in the 'set_defaults' script when a PD object (see PD objects) is inserted into a diagram.

The latest given position ID in each class is stored into the table DEFAULTS when the user selects OK in Modify Data > Edit.

Getting a Unique ID

The following functions return the next unique ID suggested by the generator via the argument list.

GEN_NEW_SYSID(string sys_id)

sys_id

The new system ID (name) to be suggested to the user to accept.

 

GEN_NEW_PLINEID(string sys_id, string pl_id)

sys_id

The name of the System to which the new pipeline ID will belong.

pl_id

The new pipeline ID.

 

GEN_NEW_ITAGID(string sys_id, string t_id)

sys_id

The name of the System to which the new instrument tag ID will belong.

t_id

The new instrument tag ID.

 

GEN_NEW_POSID(string tblname, string type, string sys, string groupname, /* inout */ listimage, typeimage)

tblname

The name of the main table.

type

The name of the sub-table

sys

The name of the System.

groupname

The name of the pipeline, electric circuit, instrument tag or empty string, depending on the main table name.

listimage

Database image of the main table.

typeimage

Database image of the sub-table.

Getting the Next Free ID

Because the user can edit the suggested ID value, it is possible that the user enters a non-unique position ID. The application checks the uniqueness, and if it is not unique it calls the script GET_UNIQ_xx to find the first free position ID of the given class.

GET_UNIQ_SYSID(/* out: */ string sys_id)

sys_id

The unique system ID to be returned back to the calling scriptlib routine.

 

GET_UNIQ_PLINEID(string system, /* out */ string pline_id)

system

The name of the System to which the new pipeline belongs.

pl_id

The unique pipeline ID to be returned to the calling scriptlib function.

 

GET_UNIQ_ITAGID(/* out: */ string system, string t_id)

system

The name of the System to which the new instrument tag belongs.

t_id

The unique instrument tag name to be returned to the calling scriptlib function.

 

GET_UNIQ_ID(string tbln, string type, string sys, string groupname, string menu_itm_data, string posid) 

tbln

The name of the main table.

type

The name of the sub-table.

sys

The name of the System.

groupname

The name of the pipeline, electric circuit, instrument tag or empty string, depending on the main table name.

menu_itm_data

Key data associated with the object selection when inserting the PD object.

posid

The unique position ID to be returned back to the calling scriptlib routine.

Incrementing the Current ID Counter Value

When a unique ID is given to a PD object, the insert routine calls a script to increase the current ID counter value.

INCREMENT_SYSID(string entered_sys_id)

entered_sys_id

The System ID whose counter is to be increased.

 

INCREMENT_PLINEID(string system, string entered_pl_id)

system

The name of the System to which the new pipeline belongs.

entered_pl_id

The pipeline ID value to be increased.

 

INCREMENT_ITAGID(string system, string entered_t_id)

system

The name of the System to which the new tag belongs.

entered_pl_id

The tag ID value to be increased.

 

INCREMENT_ID(string tbln, string type, string sys, string groupname, string menu_itm_data, string editedposid)

tbln

The name of the main table.

type

The name of the sub-table.

sys

The name of the System.

groupname

The name of the pipeline, electric circuit, instrument tag or empty string, depending on the main table name.

menu_itm_data

Key data associated with the object selection when inserting the PD object.

editedposid

The position ID string of the armature, electric circuit, equipment or instrument.

Scriptlib Functions for Position ID Generation

The script functions described above form the interface that the scriptlib functions call at run time. The project configuration defines what these functions internally do.

The scriptlib includes a set of functions for managing the generation of unique position IDs. These functions can be used in the initial setup as is done in the example environment or you can use your own tools.

The following concepts are related to the scriptlib default ID generator:

class

This is the key field in the DEFAULTS database table. The value of the class is specified in 'set_defaults'.

prefix

The prefix of the position ID string. The format of a position ID string is <prefix><ID number> where 'prefix' is a constant string inside one class and 'ID number' is an integer counter value that increases from object to object.

defaultid

The default name string to be used for example as a position ID. It is generated by concatenating the prefix with a counter value.

givenposid

The position ID the user has accepted (default ID or something else).

rec

Record ID to the record storing the default information for this class. This is typically used when the counter is increased after the user has accepted an ID string. This is to avoid multiple searches.

The system consists of the following script functions that can be called from the script functions of the 'set_defaults' script:

GET_DEFAULT_ID(STRING class, STRING prefix, STRING defaultid)

Get the next free ID.

INCREMENT_DEFAULT_ID(STRING class, STRING givenposid)

Increase the current value of the counter.

GET_FIRST_UNIQ_ID(string tbln, string class, string key field, string defaultid)

tbln

The name of the main table of the PD object the position ID of which is to be scanned.

key field

The name of the key field in the table. Typically PosId.

This function finds the first unique ID of a table. The table tbln is expected to have a key field Status.

This function calls back the function GET_POSID_FORMAT to 'set_defaults'.

All initial ID formats and counter values should be defined in that script. Formats are C printf formats.

In 'set_defaults':

GET_POSID_FORMAT(string class, string format, init_seed)

class

The class

format

Format of the ID number.

init_seed

Class-specific initial counter seed.

Managing the Instrument Menu Item Data

The 2D symbols which draw the instrument bubbles take these arguments:

  • type (location)
  • instrument text (measured variable, modifier and function)
  • position ID (instrument tag + ID number)
  • superscript (a string printed to upper right of the instrument bubble)
  • subscript (a string printed to lower right of the instrument bubble)

The type and initial instrument text are coded into the menu item data, so this coded data must be decoded prior to opening the database entry for editing.

After the user has edited the data, the application must extract the possibly modified data from the database image to update the instrument bubble label.

DECODE_INST_MENUITM(string menuitm, /* out: */ string coded_loc, string text)

menuitm

The menu item data (coded value for location and instrument text). This is a combination of the attributes of the Diagram Object Template.

coded_loc

The location code decoded out from menuitm.

text

The initial value for the instrument text.

This function decodes the type and text which is coded into the MenuItmData.

GET_INST_SYMPAR(img, /* out: */ string coded_loc, string text, string pos, string supersc, string subsc)

img

The database image of the instrument data. It includes the changes that were just made.

coded_loc

The location of the instrument as a coded value. This will be interpreted inside the 2D symbol.

text

The upper row of the instrument bubble.

pos

The lower row of the instrument bubble.

supersc

Superscript

subsc

Subscript

This function retrieves the arguments of the bubble symbol from the edited database data image.